/*jslint browser: true*/
/*global $, jQuery, alert, window*/
window.view = {};
window.view.set = {
    "train_url": function (major_id) {
        "use strict";
        $("#train_url").attr("href", "/major/{id}/index/".format({
            id: major_id
        }));
    },
    "paper_url": function (major_id) {
        "use strict";
        $("#paper_url").attr("href", "/major/{id}/paper/".format({
            id: major_id
        }));
    },
    "history_url_train": function (major_id) {
        "use strict";
        $("#history_url_train").attr("href", "/major/{id}/history/train/".format({
            id: major_id
        }));
    },
    "history_url_paper": function (major_id) {
        "use strict";
        $("#history_url_paper").attr("href", "/major/{id}/history/paper/".format({
            id: major_id
        }));
    },
    "major_title": function (major_name) {
        "use strict";
        $("#major_title").html(major_name);
    },
    "major_crumb": function (major_name) {
        "use strict";
        $("#major_crumb").html(major_name);
    },
    "exam_count": function (length_value) {
        "use strict";
        $("#paper_count").html(length_value);
    },
    "average_score": function (score_value) {
        "use strict";
        $("#aver_score").html(score_value);
        
    }
};
window.sync = {
    "exam_list_html": function (exam_list) {
        "use strict";
        var index,
            length = exam_list.length,
            exam_item,
            sum_score = 0,
            html_list = [];
        html_list.push("<tr><th>名称</th><th>答题时间</th><th>用时</th>");
        html_list.push("<th>得分</th><th>操作</th></tr>");
        for (index = 0; index < length; index += 1) {
            exam_item = exam_list[index];
            sum_score += exam_item.score;
            html_list.push("<tr>");
            html_list.push("<td>{name}</td>".format({
                name: exam_item.name
            }));
            html_list.push("<td>{date}</td>".format({
                date: window.int_to_date(exam_item.date)
            }));
            html_list.push("<td>{time_limit}</td>".format({
                time_limit: exam_item.time_limit
            }));
            html_list.push("<td>{score}</td>".format({
                score: exam_item.score
            }));
            html_list.push('<td><table class="paper_record"><tr>');
            html_list.push('<td><a href="/exam/{id}/analysis/" target="_blank">解析</a></td>'.format({
                id: exam_item.id
            }));
            html_list.push('<td><a href="/exam/{id}/incorrect/" target="_blank">错题</a></td>'.format({
                id: exam_item.id
            }));
            html_list.push('<td><a class="again" paper_id={paper_id} '.format({
                paper_id: exam_item.paper_id
            }));
            html_list.push('href="javascript:void(0)">重做</a></td>');
            html_list.push('<td><a class="delete" key="/exam/{id}/"'.format({
                id: exam_item.id
            }));
            html_list.push('href="javascript:void(0)">删除</a></td></tr></table></td>');
            html_list.push("</tr>");
        }
        $("#paper_history").html("");
        $(html_list.join("")).appendTo("#paper_history");
        window.view.set.average_score(Math.floor(sum_score / length * 100) / 100);
    }
};
window.init = {
    "global_var": function () {
        "use strict";
        var pathname = location.pathname,
            major_key,
            update_reg = new RegExp('^/major/([0-9]+)/history/(train|paper)/$'),
            reg_result_list = update_reg.exec(pathname),
            index,
            length,
            major_item;
        if (reg_result_list) {
            //update question
            window.is_valid = true;
            window.paper_all_key = "/paper/";
            window.exam_all_key = "/exam/";
            window.major_id = parseInt(reg_result_list[1], 10);
            major_key = "/major/{id}/".format({id: window.major_id});
            major_item = window.format.s2c(window.storage(major_key),
                                           window.format.major)[0];
            window.major_name =  major_item.name;
            if (reg_result_list[2] === "train") {
                window.is_train = true;
            } else {
                window.is_paper = true;
            }
        } else {
            throw new Error("网页错误");
        }
    },
    "html_page": function () {
        "use strict";
        window.view.set.train_url(window.major_id);
        window.view.set.paper_url(window.major_id);
        window.view.set.history_url_train(window.major_id);
        window.view.set.history_url_paper(window.major_id);
        window.view.set.major_title(window.major_name);
        window.view.set.major_crumb(window.major_name);
    },
    "exam_list_page": function () {
        "use strict";
        var search_data = {
                "major_id": window.major_id,
                "category_num": window.is_paper ? 2 : 1
            };
        $.ajax({
            url:  window.exam_all_key,
            type: "PUT",
            dataType: "json",
            async: false,
            data: JSON.stringify(search_data),
            success: function (result, status) {
                var exam_list = window.format.s2c(result.data,
                                                   window.format.exam);
                window.view.set.exam_count(exam_list.length);
                window.sync.exam_list_html(exam_list);
                window.hideLoading();
            }
        });
          
    },
    "addEventListener": function () {
        "use strict";
        $("#paper_history").delegate(".delete", "click", window.listener.delete_exam);
        $("#paper_history").delegate(".again", "click", window.listener.start_exam);
        window.hideLoading();
    }
};
window.init_func_list = ["global_var",
                         "html_page",
                         "exam_list_page",
                         "addEventListener"];
window.listener = {
    "delete_exam": function () {
        "use strict";
        window.loading();
        var element = $(this),
            key = element.attr("key");
        $.ajax({
            url:  key,
            type: "DELETE",
            dataType: "json",
            async: true,
            success: function (result, status) {
                window.location.reload();
            }
        });
        
    },
    "start_exam": function () {
        "use strict";
        window.loading();
        var element = $(this),
            paper_id = parseInt(element.attr("paper_id"), 10),
            paper_key = "/paper/{id}/".format({id: paper_id}),
            paper_data = window.format.s2c(window.storage(paper_key),
                                           window.format.paper)[0],
            detail_key = "/paper/detail/{id}/".format({id: paper_data.detail_id}),
            detail_data = window.format.s2c(window.storage(detail_key),
                                            window.format.detail)[0],
            exam_create_data = {
                "detail_id": detail_data.id,
                "major_id": paper_data.major_id,
                "paper_id": paper_data.id,
                "name": paper_data.name,
                "time_limit": paper_data.time_limit,
                "category_num": paper_data.category_num
            };
        $.ajax({
            url:  "/exam/",
            type: "POST",
            dataType: "json",
            async: false,
            data: JSON.stringify(exam_create_data),
            success: function (result, status) {
                var exam_data = window.format.s2c(result.data,
                                                  window.format.exam)[0];
                window.exam_key = "/exam/{exam_id}/".format({
                    exam_id: exam_data.id
                });
                window.exam_url = "/exam/{exam_id}/exam/".format({
                    exam_id: exam_data.id
                });
                window.storage(window.exam_key, result.data);
                window.open(window.exam_url);
                window.hideLoading();
            }
        });
    }

};
